package com.edu.zju.scs.panacea.service.plan;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Industry;
import com.edu.zju.scs.panacea.entity.plan.Qualification;
import com.edu.zju.scs.panacea.entity.plan.Seller;
import com.edu.zju.scs.panacea.entity.query.QualificationQuery;
import com.edu.zju.scs.panacea.entity.vo.QualificationVO;
import com.edu.zju.scs.panacea.mapper.plan.IndustryMapper;
import com.edu.zju.scs.panacea.mapper.plan.QualificationMapper;
import com.edu.zju.scs.panacea.mapper.plan.SellerMapper;
import com.edu.zju.scs.panacea.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-30 16:12
 */

@Service
public class QualificationService {

    @Autowired
    private QualificationMapper qualificationMapper;

    @Autowired
    private IndustryMapper industryMapper;

    @Autowired
    private SellerMapper sellerMapper;

    /**
     * 根据ID查询某个资质信息
     * @param id
     * @return
     */
    public BaseResult getQualification(Long id) {
        BaseResult baseResult = new BaseResult();
        Qualification qualification = qualificationMapper.selectQualification(id);
        if (qualification == null) {
            baseResult.error("无此id对应的资质");
        } else {
            baseResult.success(qualification);
        }
        return baseResult;
    }

    /**
     * 查询某商家在某行业的资质状况
     * @param qualificationQuery
     * @return
     */
    public BaseResult getIndustryQualiBySeller(QualificationQuery qualificationQuery) {
        BaseResult baseResult = new BaseResult();
        // 根据行业ID获取行业信息
        Industry industry = industryMapper.selectIndustry(qualificationQuery.getIndustryId());
        // 获取该行业所需的资质
        List<String> requiredQualifications = industry.getQualifications();

        // 获取用户的在该行业下的所有资质
        List<Qualification> qualificationList = qualificationMapper.selectSpecificQualification(qualificationQuery);
        // 将用户资质转化为map，可根据资质名称找到资质信息
        Map<String, Qualification> qualificationMap =  qualificationList.stream().collect(Collectors.toMap(Qualification::getName, a -> a,(k1, k2)->k1));

        List<QualificationVO> qualificationVOList = new ArrayList<>();
        for (String requiredQualification : requiredQualifications) {
            QualificationVO qualificationVO = new QualificationVO();
            qualificationVO.setName(requiredQualification);
            if (qualificationMap != null && qualificationMap.containsKey(requiredQualification)) {
                Qualification qualification = qualificationMap.get(requiredQualification);
                qualificationVO.setState(qualification.getState());
            } else {
                qualificationVO.setState(-1);
            }

            qualificationVOList.add(qualificationVO);
        }

        baseResult.success(qualificationVOList);
        return baseResult;
    }

    /**
     * 查询某商家的资质状况
     * @param qualificationQuery
     * @return
     */
    public BaseResult getQualificationBySeller(QualificationQuery qualificationQuery) {
        System.out.println(qualificationQuery);
        BaseResult baseResult = new BaseResult();
        // 获取用户的的所有资质
        List<Qualification> qualificationList = qualificationMapper.selectSpecificQualification(qualificationQuery);
        List<QualificationVO> qualificationVOList = new ArrayList<>();
        for (Qualification qualification : qualificationList) {
            System.out.println(qualification);
            QualificationVO qualificationVO = new QualificationVO();
            qualificationVO.setId(qualification.getId());
            qualificationVO.setState(qualification.getState());
            qualificationVO.setName(qualification.getName());
            qualificationVO.setIndustryId(qualification.getIndustryId());
            qualificationVO.setSellerId(qualification.getSellerId());
            qualificationVO.setUrl(qualification.getUrl());
            Industry industry = industryMapper.selectIndustry(qualification.getIndustryId());
            qualificationVO.setIndustryName(industry.getName());
            qualificationVOList.add(qualificationVO);
        }

        baseResult.success(qualificationVOList);
        return baseResult;
    }

    /**
     * 查询所有商家的资质状况
     * @param
     * @return
     */
    public BaseResult getAllQualification() {
        BaseResult baseResult = new BaseResult();
        // 获取所有资质
        List<Qualification> qualificationList = qualificationMapper.selectAllQualification();
        List<QualificationVO> qualificationVOList = new ArrayList<>();
        for (Qualification qualification : qualificationList) {
            QualificationVO qualificationVO = new QualificationVO();
            qualificationVO.setId(qualification.getId());
            qualificationVO.setState(qualification.getState());
            qualificationVO.setName(qualification.getName());
            qualificationVO.setIndustryId(qualification.getIndustryId());
            qualificationVO.setSellerId(qualification.getSellerId());
            qualificationVO.setUrl(qualification.getUrl());
            Industry industry = industryMapper.selectIndustry(qualification.getIndustryId());
            qualificationVO.setIndustryName(industry.getName());
            Seller seller = sellerMapper.selectSeller(qualification.getSellerId());
            qualificationVO.setSellerName(seller.getName());
            qualificationVOList.add(qualificationVO);
        }

        baseResult.success(qualificationVOList);
        return baseResult;
    }

    public BaseResult uploadImg(MultipartFile file, Long sellerId,  Long industry, String qualification) {
        BaseResult baseResult = new BaseResult();
        String contentType = file.getContentType();   //图片文件类型
        String fileName = file.getOriginalFilename();  //图片名字

        //文件存放路径
        String filePath = "/Users/mzq/IdeaProjects/panacea_file/qualification/" + sellerId + "/" + industry + "/" + qualification + "/";

        //调用文件处理类FileUtil，处理文件，将文件写入指定位置
        try {
            FileUtil.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {
            // TODO: handle exception
        }

        baseResult.success(filePath + fileName);
        return baseResult;
    }


    public BaseResult addQualification(Qualification qualification) {
        BaseResult baseResult = new BaseResult();
        // 返回插入记录条数
        Long insertNums = qualificationMapper.insertQualification(qualification);
        if (insertNums == null) {
            baseResult.error("新增资质失败！");
        } else {
            baseResult.success(insertNums);
        }
        return baseResult;
    }



    public BaseResult deleteQualification(Long id) {
        BaseResult baseResult = new BaseResult();
        long deleteDevNums = qualificationMapper.deleteQualification(id);
        if (deleteDevNums == 0) {
            baseResult.error("删除资质失败！");
        } else {
            baseResult.success(deleteDevNums);
        }
        return baseResult;
    }


}
